import datetime
import tkinter as tk
from tkinter import ttk


def create_main_ui(app):
    """创建用户界面组件"""
    # 主框架
    main_frame = tk.Frame(app, bg="#f0f0f0", padx=20, pady=15)  # 修改为使用 app 作为父组件
    main_frame.pack(fill=tk.BOTH, expand=True)

    # 标题
    title_frame = tk.Frame(main_frame, bg="#f0f0f0")
    title_frame.pack(fill=tk.X, pady=(0, 15))

    title_label = tk.Label(title_frame, text="智能定时关机工具", font=(app.font[0], 16, "bold"), bg="#f0f0f0")
    title_label.pack()

    # 计划类型区域
    create_schedule_type_frame(app, main_frame)

    # 关机方式区域
    create_shutdown_mode_frame(app, main_frame)

    # 时间设置区域
    create_time_settings_frame(app, main_frame)

    # 倒计时显示区域
    create_countdown_display(app, main_frame)

    # 按钮区域
    create_button_frame(app, main_frame)

    # 状态栏
    status_bar = tk.Label(app.root, textvariable=app.status_var, bd=3, relief=tk.SUNKEN, anchor=tk.W,
                          font=app.font)
    status_bar.pack(side=tk.BOTTOM, fill=tk.X)

    # 配置样式
    style = ttk.Style()
    style.configure("Accent.TButton", background="#2196F3")  # 蓝色
    style.configure("Danger.TButton", background="#F44336")

    # 初始化UI状态
    app.update_schedule_ui()
    app.update_shutdown_ui()


def create_schedule_type_frame(app, parent):
    schedule_frame = tk.LabelFrame(parent, text="计划类型", font=app.font, bg="#f0f0f0", padx=10, pady=10)
    schedule_frame.pack(fill=tk.X, pady=(0, 10))

    single_radio = tk.Radiobutton(schedule_frame, text="单次任务", variable=app.schedule_type, value="单次",
                                  font=app.font, bg="#f0f0f0", command=app.update_schedule_ui)
    single_radio.pack(side=tk.LEFT, padx=(0, 15))

    daily_radio = tk.Radiobutton(schedule_frame, text="每日任务", variable=app.schedule_type, value="每天",
                                 font=app.font, bg="#f0f0f0", command=app.update_schedule_ui)
    daily_radio.pack(side=tk.LEFT, padx=(0, 15))

    weekly_radio = tk.Radiobutton(schedule_frame, text="每周任务", variable=app.schedule_type, value="每周",
                                  font=app.font, bg="#f0f0f0", command=app.update_schedule_ui)
    weekly_radio.pack(side=tk.LEFT)

    # 每周选项区域（默认隐藏）
    app.weekly_frame = tk.LabelFrame(parent, text="选择执行星期", font=app.font, bg="#f0f0f0", padx=50, pady=10)
    app.weekly_frame.pack(fill=tk.X, pady=(5, 10), ipady=10)

    # 按每行4个排列星期选项
    days = list(app.weekdays.items())
    for i in range(0, len(days), 4):
        row_frame = tk.Frame(app.weekly_frame, bg="#f0f0f0")
        row_frame.pack(fill=tk.X, pady=2)
        for day, var in days[i:i + 4]:
            checkbox = tk.Checkbutton(row_frame, text=day, variable=var, font=app.font, bg="#f0f0f0")
            checkbox.pack(side=tk.LEFT, padx=(0, 15), pady=5)


def create_shutdown_mode_frame(app, parent):
    mode_frame = tk.LabelFrame(parent, text="关机方式", font=app.font, bg="#f0f0f0", padx=10, pady=10)
    mode_frame.pack(fill=tk.X, pady=(0, 10))

    app.shutdown_type = tk.StringVar(value="倒计时")

    countdown_radio = tk.Radiobutton(mode_frame, text="倒计时关机", variable=app.shutdown_type, value="倒计时",
                                     font=app.font, bg="#f0f0f0", command=app.update_shutdown_ui)
    countdown_radio.pack(side=tk.LEFT, padx=(0, 20))

    specific_radio = tk.Radiobutton(mode_frame, text="指定时间关机", variable=app.shutdown_type, value="指定时间",
                                    font=app.font, bg="#f0f0f0", command=app.update_shutdown_ui)
    specific_radio.pack(side=tk.LEFT)


def create_time_settings_frame(app, parent):
    time_frame = tk.LabelFrame(parent, text="时间设置", font=app.font, bg="#f0f0f0", padx=10, pady=10)
    time_frame.pack(fill=tk.X, pady=(0, 10))

    # 倒计时模式
    app.countdown_frame = tk.Frame(time_frame, bg="#f0f0f0")

    hours_label = tk.Label(app.countdown_frame, text="小时:", font=app.font, bg="#f0f0f0")
    hours_label.grid(row=0, column=0, padx=5, pady=5)

    app.hours_var = tk.StringVar(value="0")
    hours_spinbox = ttk.Spinbox(app.countdown_frame, from_=0, to=23, width=5, textvariable=app.hours_var,
                                font=app.font)
    hours_spinbox.grid(row=0, column=1, padx=5, pady=5)

    minutes_label = tk.Label(app.countdown_frame, text="分钟:", font=app.font, bg="#f0f0f0")
    minutes_label.grid(row=0, column=2, padx=5, pady=5)

    app.minutes_var = tk.StringVar(value="0")
    minutes_spinbox = ttk.Spinbox(app.countdown_frame, from_=0, to=59, width=5, textvariable=app.minutes_var,
                                  font=app.font)
    minutes_spinbox.grid(row=0, column=3, padx=5, pady=5)

    seconds_label = tk.Label(app.countdown_frame, text="秒:", font=app.font, bg="#f0f0f0")
    seconds_label.grid(row=0, column=4, padx=5, pady=5)

    app.seconds_var = tk.StringVar(value="0")
    seconds_spinbox = ttk.Spinbox(app.countdown_frame, from_=0, to=59, width=5, textvariable=app.seconds_var,
                                  font=app.font)
    seconds_spinbox.grid(row=0, column=5, padx=5, pady=5)

    # 指定时间模式
    app.specific_frame = tk.Frame(time_frame, bg="#f0f0f0")

    specific_label = tk.Label(app.specific_frame, text="设置关机时间:", font=app.font, bg="#f0f0f0")
    specific_label.grid(row=0, column=0, padx=5, pady=5)

    current_time = datetime.datetime.now()

    app.shutdown_hours_var = tk.StringVar(value=f"{current_time.hour:02d}")
    hours_spinbox = ttk.Spinbox(app.specific_frame, from_=0, to=23, width=5,
                                textvariable=app.shutdown_hours_var, font=app.font)
    hours_spinbox.grid(row=0, column=1, padx=5, pady=5)

    time_separator = tk.Label(app.specific_frame, text=":", font=app.font, bg="#f0f0f0")
    time_separator.grid(row=0, column=2, padx=0, pady=5)

    app.shutdown_minutes_var = tk.StringVar(value=f"{current_time.minute:02d}")
    minutes_spinbox = ttk.Spinbox(app.specific_frame, from_=0, to=59, width=5,
                                  textvariable=app.shutdown_minutes_var, font=app.font)
    minutes_spinbox.grid(row=0, column=3, padx=5, pady=5)


def create_countdown_display(app, parent):
    countdown_display_frame = tk.Frame(parent, bg="#f0f0f0")
    countdown_display_frame.pack(fill=tk.X, pady=(0, 15))

    countdown_label = tk.Label(countdown_display_frame, text="倒计时:", font=(app.font[0], 12), bg="#f0f0f0")
    countdown_label.pack(side=tk.LEFT, padx=(0, 10))

    app.countdown_var = tk.StringVar(value="00:00:00")
    countdown_value = tk.Label(countdown_display_frame, textvariable=app.countdown_var,
                              font=(app.font[0], 18, "bold"),
                              fg="#E53935", bg="#f0f0f0")
    countdown_value.pack(side=tk.LEFT)


def create_button_frame(app, parent):
    button_frame = tk.Frame(parent, bg="#f0f0f0")
    button_frame.pack(fill=tk.X, pady=(10, 0))

    start_button = ttk.Button(button_frame, text="开始执行", command=app.start_countdown, width=15,
                             style="Accent.TButton")
    start_button.pack(side=tk.LEFT, padx=(0, 15))

    cancel_button = ttk.Button(button_frame, text="取消计划", command=app.cancel_shutdown, width=15,
                              style="Danger.TButton")
    cancel_button.pack(side=tk.LEFT)